package com.hxcoder.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxcoder.commonutils.JwtUtils;
import com.hxcoder.servicebase.exceptionhandler.StarException;
import com.hxcoder.ucenter.entity.UcenterMember;
import com.hxcoder.ucenter.entity.vo.RegisterVo;
import com.hxcoder.ucenter.mapper.UcenterMemberMapper;
import com.hxcoder.ucenter.service.UcenterMemberService;
import com.hxcoder.ucenter.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 会员表 服务实现类
 *
 * @author hxcoder
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(UcenterMember member) {

        //获取登录邮件地址和密码
        String mail = member.getMail();
        String password = member.getPassword();

        //邮件地址和密码非空判断
        if (StringUtils.isEmpty(mail) || StringUtils.isEmpty(password)) {
            throw new StarException(20001, "登录失败");
        }

        //判断邮件地址是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mail", mail);
        UcenterMember mailMember = baseMapper.selectOne(wrapper);
        if (mailMember == null) {
            throw new StarException(20001, "登录失败");
        }

        //把输入的密码进行加密，再和数据库密码进行比较
        if (!MD5.encrypt(password).equals(mailMember.getPassword())) {
            throw new StarException(20001, "登录失败");
        }

        //判断用户是否禁用
        if (mailMember.getIsDisabled()) {
            throw new StarException(20001, "登录失败");
        }

        //登录成功
        //生成token字符串，使用jwt工具类
        return JwtUtils.getJwtToken(mailMember.getId(), mailMember.getNickname());
    }

    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode();
        String mail = registerVo.getMail();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        //非空判断
        if (StringUtils.isEmpty(mail) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new StarException(20001, "注册失败");
        }

        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mail);
        if (!code.equals(redisCode)) {
            throw new StarException(20001, "注册失败");
        }

        //判断邮件地址是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mail", mail);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new StarException(20001, "注册失败");
        }

        //数据添加数据库中
        UcenterMember member = new UcenterMember();
        member.setMail(mail);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://star-college.oss-cn-chengdu.aliyuncs.com/avatar/avator.jpg");

        baseMapper.insert(member);
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
